require "./SysAdd.css"
{SysWin} = require "SysWin"
{Srv} = require "Srv"

SupperClzz = SysWin
exports["@SysAdd"] = {comment:"增加页面基类"}
exports.SysAdd = new Class
  Extends: SupperClzz
  options:
    pgType: "add"
    enyStr: ""
    headArr: []
    #所有需要验证的控件
    $vdts: []
    hasChgEvt: false
  onDraw: ->
    t = this
    o = t.options
    elt = o.ele
    t.initAvalon()
    t.initThisSrv()
    t.initImgInput()
    yield t.initPg()
    t.initButEvt ["cancelBut","confirmBut"]
    t.initPgEvt()
    return
  initPg: ->
    t = this
    o = t.options
    #初始化验证
    t.initVdts()
    return
  runAllVdts: (keyArr)->
    t = this
    o = t.options
    elt = o.ele
    isPass = true
    keyArr = o.$vdts if !keyArr
    for key in keyArr
      iez = elt.getE "[h:iez='"+key+"']"
      iezWg = iez.wg()
      isPass = yield iezWg.runVdts()
      if !isPass
        iezWg.focusErrBox()
        return false
    isPass
  "@initVdt":{comment:"初始化验证"}
  initVdts: (keyArr)->
    t = this
    o = t.options
    elt = o.ele
    keyArr = o.$vdts if !keyArr
    eny = undefined
    if o.pgType is "edit"
      eny = elt.retrieve "eny"
    for key in keyArr
      iez = elt.getE "[h:iez='"+key+"']"
      iezWg = iez.wg()
      iezWg.addVdt ->
        iezTmp = $ this
        keyTmp = iezTmp.get "h:iez"
        valTmp = this.getVal {cmpOldVal:false}
        vdtCbFn = t[keyTmp+"Vdt"]
        if vdtCbFn
          args = [keyTmp,valTmp]
          #控件验证时,执行回调方法
          args.push eny.id if o.pgType is "edit"
          yield vdtCbFn.apply t,args
    return
  initThisSrv: ->
    t = this
    o = t.options
    elt = o.ele
    clz = "sys."+o.enyStr+"AddSrv"
    
    prnClz = ""
    prnWinArr = elt.getParents "[h:pg]"
    for i in [0...prnWinArr.length]
      prnWin = prnWinArr[prnWinArr.length-1-i]
      continue if prnWin is document.body
      prnPg = prnWin.get "h:pg"
      prnClz += prnPg+"."
    clz = prnClz+clz+"||"+clz if prnClz
    
    o.thisSrv = new Srv clz:clz
    o.thisSrv.options.ele = elt
    return
  "@initPgEvt":{comment:"初始化页面按钮事件,例如ctrl+s 保存"}
  initPgEvt: ->
    t = this
    o = t.options
    elt = o.ele
    elt.addEvent "click",(e)->
      e.preventDefault()
      return
    elt.addEvent "keydown",(e)->
      code = e.code
      if e.control and code is 83
        e.stop()
        confirmBut = elt.getE ".confirmBut"
        co t.confirmButClk confirmBut
      else if code is 27
        e.stop()
        cancelBut = elt.getE ".cancelBut"
        co t.cancelButClk cancelBut
      return
    elt.addEvent "change",(e)->
      o.hasChgEvt = true
      return
    return
  "@aftDoModal":{comment:"模态窗口显示之后调用此方法"}
  aftDoModal: ->
    t = this
    o = t.options
    elt = o.ele
    #调整模态窗口显示的位置
    left = 0
    top = 0
    do_modal_win = elt.getParent ".do_modal_win"
    do_modal_div = do_modal_win.getParent ".do_modal_div"
    bdSz = do_modal_div.measure -> this.getSize()
    winSz = do_modal_win.measure -> this.getSize()
    left = (bdSz.x-winSz.x)/2
    top = (bdSz.y-winSz.y)/3
    do_modal_win.setStyles {left:left,top:top,"max-width":bdSz.x,"max-height":bdSz.y}
    t.frtElFcu()
    return
  "@frtElFcu":{comment:"first element focus 页面打开后第一个控件获得焦点"}
  frtElFcu: ->
    t = this
    o = t.options
    elt = o.ele
    iez = elt.getE "input[h:iez]:not([readonly])"
    iez.focus() if iez
    return
  confirmButClk: (but,e,headArr)->
    t = this
    o = t.options
    elt = o.ele
    e.preventDefault() if e
    but.set "disabled",true if but
    isPass = yield t.runAllVdts()
    #验证不通过
    if !isPass
      but.set "disabled",false if but
      return
    eny = {}
    headArr = headArr or o.headArr
    yield t.getPgVal eny,{cmpOldVal:false}
    rltSet = yield t.confirmRst eny,headArr
    but.set "disabled",false if but
    yield t.aftSave rltSet
    return
  confirmRst: (eny,headArr)->
    t = this
    o = t.options
    rltSet = yield o.thisSrv.ajax "confirmButClk",[eny,headArr]
    rltSet
  "@aftSave": {comment:"保存成功之后,关闭页面"}
  aftSave: (rltSet)->
    t = this
    o = t.options
    elt = o.ele
    if rltSet
      window.ncWg.addNotice "info","保存成功!",2
      cancelBut = elt.getE ".cancelBut"
      yield t.cancelButClk cancelBut,undefined,false
    return
  cancelButClk: (but,e,isCkd)->
    t = this
    o = t.options
    elt = o.ele
    e.preventDefault() if e
    if isCkd isnt false
      if o.hasChgEvt
        return if !window.confirm "数据尚未保存!是否继续退出!"
    do_modal_div = elt.getParent ".do_modal_div"
    but.set "disabled",true if but
    yield t.closePage()
    but.set "disabled",false if but
    do_modal_div.destroy()
    return
  closePage: ->
    t = this
    o = t.options
    yield SupperClzz.prototype.closePage.apply t,arguments
    return
